package com.tjakus.webpage;

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import com.tjakus.logging.Logging;

/**
 * This class represents a web page downloader
 * 
 * @author tjakus
 */
public class WebpageDownloader {

	private StringBuilder html = new StringBuilder();

	/**
	 * Downloads HTML data from a WEB page and return it as a string
	 * 
	 */
	public String retrieveAndGetHtmlPage(String webPageUrl) {
		HttpURLConnection httpUrlConnection;
		Logging.getLogger().info(
				"WebpageDownloader - WebpageDownloader [ " + webPageUrl + " ]");
		try {
			URL url = new java.net.URL(webPageUrl);
			httpUrlConnection = (HttpURLConnection) url.openConnection();
			//httpUrlConnection.setRequestProperty("Accept-Charset", "UTF-8");
			//httpUrlConnection.setRequestProperty("Content-Type",
			//		"application/x-www-form-urlencoded;charset=utf-8");
			BufferedInputStream in = new BufferedInputStream(
					httpUrlConnection.getInputStream());
			Reader r = new InputStreamReader(in, "UTF-8");

			int i;
			while (((i = r.read()) != -1)) {
				html.append((char) i);
			}
			html.trimToSize();
			//System.out.println(html.substring(0));
			return html.substring(0);
		} catch (IOException e) {
			Logging.getLogger().error(
					"WebpageDownloader - WebpageDownloader [ UNABLE TO DOWNLOAD ]", e);
			return null;
		}
	}
}
